﻿namespace Chain.Wechat
{
    using Chain.BLL;
    using Chain.Model;
    using System;
    using System.Runtime.CompilerServices;
    using System.Xml;

    public class MenuRequest : EventRequest
    {
        
        private string _EventKey;

        public MenuRequest(XmlDocument input) : base(input)
        {
            XmlNode root = base.XmlDoc.FirstChild;
            this.EventKey = root["EventKey"].InnerText;
            base.RequestEvent += new IRequest.RequestEventHandler(this.MenuReqeustEventHandler);
        }

        public string MenuReqeustEventHandler()
        {
            string responseText;
            LogManager.Remove(base.FromUserName);
            if (!(base.Event == "CLICK"))
            {
                bool flag1 = base.Event == "VIEW";
                return string.Empty;
            }
            Chain.BLL.Mem bllMem = new Chain.BLL.Mem();
            Chain.Model.Mem mem = bllMem.GetMemByWeiXinCard(base.FromUserName);
            string name1 = this.EventKey;
            if (name1 == null)
            {
                goto Label_0281;
            }
            if (!(name1 == "1"))
            {
                if (name1 == "2")
                {
                    if (mem != null)
                    {
                        return new TextResponse(this, "您已经绑定了微信会员!").Result();
                    }
                    TransformEntryNumberLog log = new TransformEntryNumberLog(0);
                    LogManager.Add(base.FromUserName, log);
                    return new TextResponse(this, "您已进入转微信会员模式,请发送您的卡号!").Result();
                }
                if (name1 == "3")
                {
                    if (mem == null)
                    {
                        return new TextResponse(this, "您目前还不是微会员,只有微会员才可以访问微网站!").Result();
                    }
                    Chain.Model.SysParameter modelSysPara = new Chain.BLL.SysParameter().GetModel(1);
                    string title = string.Format("点击进入{0}微网站", modelSysPara.WeiXinShopName);
                    return new BusinessLogic().GetMemCardResponse(this, mem, title);
                }
                if (name1 == "4")
                {
                    if (mem == null)
                    {
                        return new TextResponse(this, "您还未申请或绑定会员卡!").Result();
                    }
                    switch (new BusinessLogic().MemSign(mem))
                    {
                        case -3:
                            return new TextResponse(this, "您今日已经签到过，重复签到不会再次赠送积分!").Result();

                        case -2:
                            return new TextResponse(this, "对不起，签到送积分活动未开启!").Result();

                        case -1:
                            return new TextResponse(this, "您还未申请或绑定会员卡!").Result();

                        case 0:
                            goto Label_01D1;

                        case 1:
                            mem = bllMem.GetMemByWeiXinCard(base.FromUserName);
                            return new BusinessLogic().GetMemCardResponse(this, mem, "今日签到成功!");
                    }
                    goto Label_01D1;
                }
                if (name1 == "5")
                {
                    if (mem == null)
                    {
                        return new TextResponse(this, "您目前还不是微会员!").Result();
                    }
                    if (mem.MemWeiXinCard == "")
                    {
                        return new TextResponse(this, "此微信没有绑定账号!").Result();
                    }
                    new Chain.BLL.MicroWebsiteSceneStr().Delete(base.FromUserName);
                    Chain.BLL.Mem membll = new Chain.BLL.Mem();
                    Chain.Model.Mem memmodel = membll.GetMemWeiXinCardModel(base.FromUserName, "MemWeiXinCard");
                    if (memmodel != null)
                    {
                        memmodel.MemWeiXinCard = null;
                        membll.Update(memmodel);
                    }
                    return new TextResponse(this, "解绑成功!").Result();
                }
                goto Label_0281;
            }
            if (mem != null)
            {
                return new TextResponse(this, "您已是微信会员!").Result();
            }
            RegisterEntryNumberLog log1 = new RegisterEntryNumberLog(0);
            LogManager.Add(base.FromUserName, log1);
            return new TextResponse(this, "您已进入会员申请模式,请发送您的手机号码!").Result();
        Label_01D1:
            return new TextResponse(this, "系统出现未知错误!").Result();
        Label_0281:
            responseText = new BusinessLogic().GetResponseByRule(this, this.EventKey);
            if (!string.IsNullOrEmpty(responseText))
            {
                return responseText;
            }
            return string.Empty;
        }

        public string EventKey
        {
            
            get
            {
                return this._EventKey;
            }
            
            set
            {
                this._EventKey = value;
            }
        }
    }
}

